package com.sm.healthy.ext.bindAdapter

import android.annotation.SuppressLint
import android.text.*
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import me.hgj.jetpackmvvm.ext.view.textString

/**
 * 描述　: 自定义 BindingAdapter
 */

object CustomBindAdapter {


    @BindingAdapter(value = ["goBack"])
    @JvmStatic
    fun goBack(view: Toolbar,listener: Callback) {
        view.setNavigationOnClickListener {
            listener.onClick()
        }
    }

    @BindingAdapter(value = ["setSelect"])
    @JvmStatic
    fun setSelect(view: View,select: Boolean) {
        view.isSelected = select
    }

    @SuppressLint("ResourceAsColor")
    @BindingAdapter(
        value = ["firstText","secondText","firstColor","secondColor"],
        requireAll = true
    )
    @JvmStatic
    fun setText(
        view: TextView,
        firstText: String,
        secondText: String,
        firstColor: Int,
        secondColor: Int
    ) {
        view.text = SpannableStringBuilder().apply {
            append(firstText)
            setSpan(
                ForegroundColorSpan(firstColor),
                0,
                length,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )

            val offset = length + 1
            append(secondText)
            setSpan(
                ForegroundColorSpan(secondColor),
                offset,
                length,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
        }
    }

    @BindingAdapter(value = ["showPwd"])
    @JvmStatic
    fun showPwd(view: EditText,boolean: Boolean) {
        if (boolean) {
            view.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
        } else {
            view.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
        }
        view.setSelection(view.textString().length)
    }

    @BindingAdapter(value = ["imageUrl"])
    @JvmStatic
    fun imageUrl(view: ImageView,url: String) {
        Glide.with(view.context.applicationContext)
            .load(url)
            .transition(DrawableTransitionOptions.withCrossFade(500))
            .into(view)
    }


    @BindingAdapter(value = ["afterTextChanged"])
    @JvmStatic
    fun EditText.afterTextChanged(action: (String) -> Unit) {
        addTextChangedListener(object: TextWatcher {
            override fun afterTextChanged(s: Editable?) {
            }

            override fun beforeTextChanged(s: CharSequence?,start: Int,count: Int,after: Int) {

            }

            override fun onTextChanged(s: CharSequence?,start: Int,before: Int,count: Int) {
                action(s.toString())
            }
        })
    }

    interface Callback {
        fun onClick()
    }
}